angular.module('underscore', []).factory('_', ['$window', function($window) {
    return $window._; // assumes underscore has already been loaded on the page
}]);

angular.module('starter.services', [])

.factory('Chats', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  var chats = [{
    id: 0,
    name: 'Ben Sparrow',
    lastText: 'You on your way?',
    face: 'img/ben.png'
  }, {
    id: 1,
    name: 'Max Lynx',
    lastText: 'Hey, it\'s me',
    face: 'img/max.png'
  }, {
    id: 2,
    name: 'Adam Bradleyson',
    lastText: 'I should buy a boat',
    face: 'img/adam.jpg'
  }, {
    id: 3,
    name: 'Perry Governor',
    lastText: 'Look at my mukluks!',
    face: 'img/perry.png'
  }, {
    id: 4,
    name: 'Mike Harrington',
    lastText: 'This is wicked good ice cream.',
    face: 'img/mike.png'
  }];

  return {
    all: function() {
      return chats;
    },
    remove: function(chat) {
      chats.splice(chats.indexOf(chat), 1);
    },
    get: function(chatId) {
      for (var i = 0; i < chats.length; i++) {
        if (chats[i].id === parseInt(chatId)) {
          return chats[i];
        }
      }
      return null;
    }
  };
})
.service('userService',function($http,$q){

  var userService = {};
  userService.setLogin=function(phone,token,memberId){
    window.localStorage.isLogin=true;
    window.localStorage.phone=phone;
    window.localStorage.token=token;
    window.localStorage.memberId=memberId;
  }

  userService.setLogout=function(){
    window.localStorage.isLogin=false;
    window.localStorage.removeItem('phone');
    window.localStorage.removeItem('token');
    window.localStorage.removeItem('memberId');
  }

  userService.getUserInfo=function(memberId){
    var deferred = $q.defer();
    var promise = deferred.promise;
    $http({
        method:'get',
        url:window.localStorage.base_url+'/app/member/getUserInfo?memberId='+memberId+'&token='+window.localStorage.token
    }).success(function(data,status,headers,config){
        deferred.resolve(data);//get successed
    }).error(function(data,status,headers,config){
        deferred.reject();//get error
    })
    return promise;
  }
  return userService;
})

/* payService @alexlee */
.service('payService', function() {
  var payDatas = {};
  payDatas.set = function(payData){
    payDatas = payData;
  };
  payDatas.get = function(){
    return payDatas;
  };
  return payDatas;
}) 

/* 产品列表 @alexlee */
.service('productsService', function($http,$q) {
  function get(id) {
    var deferred = $q.defer();
    var promise = deferred.promise;
    $http({
        method:'get',
        url: id==2?window.localStorage.base_url+'/app/product/getPrimaryProduct' : window.localStorage.base_url+'/app/product/find?moduleId='+id
    }).success(function(data,status,headers,config){
        deferred.resolve(data);//get successed
    }).error(function(data,status,headers,config){
        deferred.reject();//get error
    })
    return promise;
  }
  return {get: get}
})

/* 投资分析 @alexlee */
.service('tzxfService', function() {
  var tzxfDatas = {};
  tzxfDatas.set = function(tzxfData){
    if(tzxfData == null){
      tzxfDatas = {};//清空
    }else{
      tzxfDatas = tzxfData;      
    }
  };
  tzxfDatas.get = function(){
    return tzxfDatas;
  };
  return tzxfDatas;
})

.service('lcghService', function() {
  var lcghDatas = {};
  lcghDatas.set = function(lcghData){
    if(lcghData == null){
      lcghDatas = {};//清空
    }else{
      lcghDatas = lcghData;      
    }

  };
  lcghDatas.get = function(){
    return lcghDatas;
  };
  return lcghDatas;
})

.service('lawService', function() {
  var lawDatas = {};
  lawDatas.set = function(lawData){
    if(lawData == null){
      lawDatas = {};//清空
    }else{
      lawDatas = lawData;      
    }
  };
  lawDatas.get = function(){
    return lawDatas;
  };
  return lawDatas;
})

.service('bankcardService', function() {
  var bankcardDatas = {};
  bankcardDatas.set = function(bankcardData){
    if(bankcardData == null){
      bankcardDatas = {};//清空
    }else{
      bankcardDatas = bankcardData;      
    }
  };
  bankcardDatas.get = function(){
    return bankcardDatas;
  };
  return bankcardDatas;
});